package com.ooqiu.gaming.ui.setting;

import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.Switch;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.blankj.utilcode.util.AppUtils;
import com.blankj.utilcode.util.FileUtils;
import com.ooqiu.gaming.R;
import com.ooqiu.gaming.base.BaseActivity;
import com.ooqiu.gaming.ui.example.VideoActivity;
import com.ooqiu.gaming.utils.SettingPrefUtil;
import com.ooqiu.gaming.view.Topbar;
import com.ooqiu.gaming.view.option_item.OptionItem;
import com.tencent.bugly.beta.Beta;

import java.io.File;
import java.text.DecimalFormat;

import butterknife.BindView;
import butterknife.ButterKnife;

@Route(path = SettingActivity.PATH)
public class SettingActivity extends BaseActivity implements CompoundButton.OnCheckedChangeListener {
    public static final String PATH = "/gn/setting";
    @BindView(R.id.topbar)
    Topbar mTopbar;
    @BindView(R.id.option_set_push)
    OptionItem mOptionSetPush;
    @BindView(R.id.option_version)
    OptionItem mOptionVersion;
    @BindView(R.id.swich_gif)
    Switch mSwichGif;
    @BindView(R.id.swich_video)
    Switch mSwichVideo;
    @BindView(R.id.swich_wifi)
    Switch mSwichWifi;
    @BindView(R.id.option_cache)
    OptionItem mOptionCache;

    @Override
    public int initContentView() {
        return R.layout.activity_setting;
    }

    @Override
    public void initInjector() {
        ButterKnife.bind(this);

//        GlideApp.getPhotoCacheDir(mContext).length();


    }

    @Override
    public void initUiAndListener() {

        mSwichGif.setChecked(SettingPrefUtil.isGifAutoPlay(mContext));
        mSwichVideo.setChecked(SettingPrefUtil.isVideoAutoPlay(mContext));
        mSwichWifi.setChecked(SettingPrefUtil.isAllow4G(mContext));

        mSwichGif.setOnCheckedChangeListener(this);
        mSwichVideo.setOnCheckedChangeListener(this);

        mOptionVersion.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                getRouter().build(VideoActivity.PATH).navigation();
                return true;
            }
        });

        mSwichWifi.setOnCheckedChangeListener(this);

        mOptionSetPush.setOnItemClickListener(new OptionItem.OnItemClickListener() {
            @Override
            public void onItemClick(OptionItem item) {
                getRouter().build(PushSettingActivity.PATH).navigation();
            }
        });

        mOptionVersion.showNotice(Beta.getUpgradeInfo() != null);

        mOptionVersion.setSubText("v" + AppUtils.getAppVersionName());


        mOptionCache.setSubText(String.valueOf(new DecimalFormat("##0.00").format((float) FileUtils.getDirLength(getExternalCacheDir()) / (float) (1024 * 1024))) + "MB");

        mOptionCache.setOnItemClickListener(new OptionItem.OnItemClickListener() {
            @Override
            public void onItemClick(OptionItem item) {
                FileUtils.deleteDir(new File(getExternalCacheDir().getAbsolutePath() + File.separator + "glide"));
                mOptionCache.setSubText(String.valueOf((float) FileUtils.getDirLength(getExternalCacheDir()) / (float) (1024 * 1024)) + "MB");

            }
        });

        mOptionVersion.setOnItemClickListener(new OptionItem.OnItemClickListener() {
            @Override
            public void onItemClick(OptionItem item) {
                Beta.checkUpgrade();
            }
        });
    }

    @Override
    protected boolean isApplyStatusBarTranslucency() {
        return false;
    }

    @Override
    protected boolean isApplyStatusBarColor() {
        return true;
    }

    @Override
    protected Toolbar toolbarIdRes() {
        return mTopbar;
    }

    @Override
    protected View statusLayoutId() {
        return null;
    }

    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        switch (buttonView.getId()) {
            case R.id.swich_gif:
                SettingPrefUtil.setGifAutoPlay(mContext, isChecked);
                break;
            case R.id.swich_video:
                SettingPrefUtil.setVideoAutoPlay(mContext, isChecked);
                break;
            case R.id.swich_wifi:
                SettingPrefUtil.setAllow4G(mContext, isChecked);
                break;
        }
    }
}
